from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from base.base_page import BasePage
import allure

class PreferencePage(BasePage):
    # 偏好设置主页面
    PAGE_TITLE = (AppiumBy.XPATH, "//android.widget.TextView[@text='偏好设置']")
    SUB_MENU = {
        "收货偏好": (AppiumBy.XPATH, "//android.widget.TextView[@text='收货偏好设置']"),
        "运单加密": (AppiumBy.XPATH, "//android.widget.TextView[@text='运单加密']"),
        "消息偏好": (AppiumBy.XPATH, "//android.widget.TextView[@text='接收信息偏好']"),
        "电话勿扰": (AppiumBy.XPATH, "//android.widget.TextView[@text='电话勿扰']"),
        "分享运单": (AppiumBy.XPATH, "//android.widget.TextView[@text='分享运单设置']")
    }

    # 收货偏好设置页
    DELIVERY_SAVE_BTN = (AppiumBy.XPATH, "//android.widget.TextView[@text='保存']")
    DELIVERY_SWITCHES = {
        "周六不收": (AppiumBy.XPATH, "//android.widget.TextView[@text='周六不收货']/following-sibling::android.widget.Switch"),
        "周日不收": (AppiumBy.XPATH, "//android.widget.TextView[@text='周日不收货']/following-sibling::android.widget.Switch"),
        "节假日不收": (AppiumBy.XPATH, "//android.widget.TextView[@text='法定节假日不收货']/following-sibling::android.widget.Switch"),
        "时间段收货": (AppiumBy.XPATH, "//android.widget.TextView[@text='每天收货时间段']/following-sibling::android.widget.Switch")
    }

    # 运单加密设置页
    WAYBILL_SAVE_BTN = (AppiumBy.XPATH, "//android.widget.TextView[@text='保存']")
    WAYBILL_OPTIONS = {
        "寄件人姓名": (AppiumBy.XPATH, "//android.widget.TextView[@text='寄件人姓名']"),
        "寄件公司": (AppiumBy.XPATH, "//android.widget.TextView[@text='寄件公司']"),
        "寄件电话": (AppiumBy.XPATH, "//android.widget.TextView[@text='寄件电话']"),
        "寄件地址": (AppiumBy.XPATH, "//android.widget.TextView[@text='寄件地址']")
    }

    # 消息偏好设置页
    MESSAGE_SWITCH = (AppiumBy.XPATH, "//android.widget.TextView[@text='个人接收偏好']/following-sibling::android.widget.Switch")

    # 电话勿扰设置页
    CALL_SAVE_BTN = (AppiumBy.XPATH, "//android.widget.TextView[@text='保存']")
    CALL_SWITCHES = {
        "夜间免打扰": (AppiumBy.XPATH, "//android.widget.TextView[@text='夜间免打扰']/following-sibling::android.widget.Switch"),
        "工作日勿扰": (AppiumBy.XPATH, "//android.widget.TextView[@text='工作日勿扰']/following-sibling::android.widget.Switch"),
        "周末勿扰": (AppiumBy.XPATH, "//android.widget.TextView[@text='周末或节假日勿扰']/following-sibling::android.widget.Switch")
    }

    # 分享运单设置页
    SHARE_SAVE_BTN = (AppiumBy.XPATH, "//android.widget.TextView[@text='保存']")
    SHARE_SWITCHES = {
        "展示寄件人姓名": (AppiumBy.XPATH, "//android.widget.TextView[@text='展示寄件人姓名']/following-sibling::android.widget.Switch"),
        "展示收件人姓名": (AppiumBy.XPATH, "//android.widget.TextView[@text='展示收件人姓名']/following-sibling::android.widget.Switch")
    }

    # ---------------------- 主页面操作 ----------------------
    def enter_sub_menu(self, menu_name):
        """进入子菜单（收货偏好/运单加密等）"""
        self.click(self.SUB_MENU[menu_name])
        WebDriverWait(self.driver, 3).until(
            EC.visibility_of_element_located(self.PAGE_TITLE)  # 替换为子页面标题定位
        )

    # ---------------------- 收货偏好设置 ----------------------
    def set_delivery_preference(self, switches):
        """
        设置收货偏好
        :param switches: 字典，key 为 DELIVERY_SWITCHES 的 key，value 为 True/False
        """
        for switch_name, is_enabled in switches.items():
            self.click(self.DELIVERY_SWITCHES[switch_name])
            # 验证开关状态（可选）
            assert self.is_switch_enabled(self.DELIVERY_SWITCHES[switch_name]) == is_enabled, \
                f"{switch_name} 开关状态与预期不符"
        self.click(self.DELIVERY_SAVE_BTN)

    def is_switch_enabled(self, switch_locator):
        """判断开关是否开启"""
        return self.find_element(switch_locator).get_attribute("checked") == "true"

    # ---------------------- 运单加密设置 ----------------------
    def set_waybill_encryption(self, options):
        """
        设置运单加密
        :param options: 字典，key 为 WAYBILL_OPTIONS 的 key，value 为加密方式（如“加密”）
        """
        for option_name, value in options.items():
            self.click(self.WAYBILL_OPTIONS[option_name])
            # 假设弹窗选择加密方式（需补充实际逻辑）
            self.driver.find_element(AppiumBy.XPATH, f"//android.widget.TextView[@text='{value}']").click()
        self.click(self.WAYBILL_SAVE_BTN)

    # ---------------------- 消息偏好设置 ----------------------
    def set_message_preference(self, is_enabled):
        """设置个人接收偏好开关"""
        self.click(self.MESSAGE_SWITCH)
        assert self.is_switch_enabled(self.MESSAGE_SWITCH) == is_enabled, \
            "消息偏好开关状态与预期不符"

    # ---------------------- 电话勿扰设置 ----------------------
    def set_call_preference(self, switches):
        """
        设置电话勿扰
        :param switches: 字典，key 为 CALL_SWITCHES 的 key，value 为 True/False
        """
        for switch_name, is_enabled in switches.items():
            self.click(self.CALL_SWITCHES[switch_name])
            assert self.is_switch_enabled(self.CALL_SWITCHES[switch_name]) == is_enabled, \
                f"{switch_name} 开关状态与预期不符"
        # 若有保存按钮需点击：self.click(self.CALL_SAVE_BTN)

    # ---------------------- 分享运单设置 ----------------------
    def set_share_preference(self, switches):
        """
        设置分享运单
        :param switches: 字典，key 为 SHARE_SWITCHES 的 key，value 为 True/False
        """
        for switch_name, is_enabled in switches.items():
            self.click(self.SHARE_SWITCHES[switch_name])
            assert self.is_switch_enabled(self.SHARE_SWITCHES[switch_name]) == is_enabled, \
                f"{switch_name} 开关状态与预期不符"
        # 若有保存按钮需点击：self.click(self.SHARE_SAVE_BTN)